/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} k
 * @return {ListNode}
 */

var rotateRight = function (head, k) {
    if (!head || k == 0)
        return head
    let pre = head
    let end = head.next
    let n = 1
    while (end != null) {
        pre = end
        end = end.next
        n++
    }
    pre.next = head
    end = head
    k = k % n
    let count = n - k
    while (count-- > 0) {
        pre = end
        end = end.next
    }
    pre.next = null
    return end
};